<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card class="tree-card">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="角色管理" name="first"
            >角色管理
            <div class="button">
              <el-button type="primary" @click.native="buttonForm"
                >新增角色</el-button
              >
            </div>
            <el-table :data="tableData" style="width: 100%">
              <!-- <el-table-column 列：通过prop绑定数据中的键 -->
              <!-- <列名通过label来设置 -->
              <el-table-column
                type="index"
                :index="indexFn(index)"
                label="序号"
                width="180"
              >
              </el-table-column>
              <el-table-column prop="name" label="名称" width="180">
              </el-table-column>
              <el-table-column prop="description" label="描述" width="180">
              </el-table-column>
              <el-table-column label="操作" width="280">
                <!-- elementul中作用域插槽的用法 -->
                <template #default="{ row }">
                  <el-button type="success" size="small">分配权限</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="editRolelbtn(row.id)"
                    >编辑</el-button
                  >
                  <el-button
                    type="danger"
                    size="small"
                    @click="delRouel(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!--
  属性:
    1. current-page 标记当前页 (谁高亮)
    2. page-sizes 可供选择的每页条数列表
    3. page-size 起作用的每页条数
    4. layout 布局, 控制是展示的控件列表布局
    5. total 总条数
  方法:
    @size-change="handleSizeChange" 每页条数变化
    @current-change="handleCurrentChange" 当前页变化
-->
            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              v-loading="loading"
            />
          </el-tab-pane>
          <el-tab-pane label="公司配置" name="second"
            >公司信息
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form ref="form" :rules="rules" :model="form" label-width="120px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="code">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button type="primary" @click="submitRole"> 确 定 </el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  jiaoseListApi,
  addListApi,
  delListApi,
  getdetalListApi,
  postdetalListApi
} from '@/api/setting'
import { regetcompangApi } from '@/api/company'
export default {
  data() {
    return {
      loading: false,
      activeName: 'first',
      tableData: [],
      currentPage: 1,
      total: 0, // 总条数
      page: 1, //当前页码
      pagesize: 5, //每页数据条数
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      companyForm: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ],
        description: [
          {
            required: true,
            message: '请输入描述名称',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  async created() {
    this.getRoleList()
    const { data } = await regetcompangApi(
      this.$store.state.user.userInfo.companyId
    )
    console.log(data)
    this.companyForm = data
  },
  computed: {
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  methods: {
    //编辑详情
    async editRolelbtn(id) {
      this.showDialog = true
      const { data } = await getdetalListApi(id)
      console.log(data)
      this.form = data
    },
    //删除角色
    delRouel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续？', '温馨提示')
        .then(async () => {
          await delListApi(id)
          //判断当前数据长度是不是等于1，和
          if (this.tableData.length === 1 && this.page > 1) {
            this.page--
          }
          //重新渲染列表
          this.getRoleList()
        })
        .catch(() => {
          console.log('点击了取消')
        })
    },
    //新增部门
    submitRole() {
      this.$refs.form.validate(async (valid, val) => {
        if (!valid) return
        //如果点击的是编辑
        if (this.form.id) {
          await postdetalListApi(this.form)
          this.$message.success('编辑成功')
        } else {
          await addListApi(this.form)
          this.$message.success('添加成功')
        }
        this.showDialog = false
        //重新渲染列表
        this.getRoleList()
        this.form = {
          name: '',
          description: ''
        }
        this.$refs.form.resetFields()
      })
    },
    buttonForm() {
      this.showDialog = true
    },
    //关闭弹层
    closeDialog() {
      this.showDialog = false
      this.form = {
        name: '',
        description: ''
      }
    },
    indexFn(index) {
      console.log(index)
      return index + 1 + (this.page - 1) * this.pagesize
    },
    handleClick(tab, event) {
      console.log(tab, event)
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 更新每页条数
      this.pagesize = val
      // 重置当前页
      this.page = 1
      // 调用方法, 重新请求
      this.getRoleList()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 更新当前页
      this.page = val
      // 调用方法, 重新请求渲染
      this.getRoleList()
    },
    async getRoleList() {
      this.loading = true
      let { data } = await jiaoseListApi(this.page, this.pageSize)
      this.total = data.total
      this.tableData = data.rows
      this.loading = false
    }
  }
}
</script>
<style scoped>
.button {
  margin-top: 15px;
}
</style>
